package main

import (
	"fmt"
	"math"
	"reflect"
	"strconv"
	"unsafe"
)

func main() {
	//arrTest()
	//reflectTest()
	//create()
	//var f = func(a, b int) {
	//	fmt.Println(a + b)
	//}
	//f(1,2)
	//var  a =1
	//fmt.Println(reflect.TypeOf(a))
	//fmt.Printf("%T",a)
	//fmt.Println()
	//fmt.Printf("%d",unsafe.Sizeof(a))
	//float_compare()
	//var x int32
	//y := 1
	//x = int32(y)
	//fmt.Println(x)
	//byteTest()
	//strConvert()
}
func float_compare() {
	x := 1.0
	y := 2.0
	fmt.Println(math.Dim(x, y))
}

func byteTest() {
	var a byte = 1
	fmt.Println(a)
	a = 'a'
	// 97 输出ASCII
	fmt.Println(a)
	// 输出对应的unicode值，即a本身的值：a
	fmt.Printf("%c", a)
	fmt.Println()

	var word int = '家'
	fmt.Println(word)
	var wordint = 23478
	fmt.Printf("%c", wordint)
}

/**
各种数据类型的创建
*/
func create() {
	//var a int
	//var b string
	//var c [3]int //数组
	var d []int // 数组切片
	d = append(d, 1)
	fmt.Println(d)
	//var map1 map[string]int // map key:string value:int

}

/**
1.bool类型
*/
func boolTest() {
	//1.bool
	var a bool
	println(a)
	a = true
	fmt.Println(a)
	fmt.Println(unsafe.Sizeof(a))
}

/*

 */

func baseTest() {
	var a int = 2
	fmt.Println(a)
	var b float64 = 4
	var c float32 = 4
	fmt.Println(b)
	fmt.Println(c)

	// 字符串
	var str string = "abc"
	fmt.Println(str)
}

/**
数组基本语法
*/
func arrTest() {
	var attr [4]string
	attr[0] = "a"
	attr[1] = "b"
	attr[2] = "c"
	fmt.Println(attr)
	for i := 0; i < len(attr); i++ {
		fmt.Println(attr[i])
	}

}

/**
检查变量类型
*/
func reflectTest() {

	var s string = "a"
	var a int = 1
	var b float64 = 4
	fmt.Println(reflect.TypeOf(s))
	fmt.Println(reflect.TypeOf(a))
	fmt.Println(reflect.TypeOf(b))
}

/**
  类型转换
*/
func strConvert() {
	//var i = 99999999
	//// int -> float
	//f := float64(i)
	//vInt32 := int8(i)
	//fmt.Println(f)
	//fmt.Println(vInt32)

	// 1.string -> bool
	var str string = "true"
	b, err := strconv.ParseBool(str)
	fmt.Println(b)
	//nil
	fmt.Println(err)

	// bool -> string
	var boolA = false
	strA := strconv.FormatBool(boolA)
	fmt.Println(strA)
	strbool := fmt.Sprintf("%t", boolA)
	fmt.Println("Springf bool->string:" + strbool)

	//2. char
	var charA byte = 'a'
	fmt.Println("Springf char -> string:" + fmt.Sprintf("%c", charA))

	// 3.int <-> string
	var vint = 1
	str = strconv.Itoa(vint)
	fmt.Println("int -> string : " + str)
	sprintf := fmt.Sprintf("%d", vint)
	fmt.Println("Springf方式转换字符串:" + sprintf)
	// base参数范围在2-36之间，10表示十进制,其实就看作进制转换
	formatInt := strconv.FormatInt(int64(vint), 10)
	fmt.Println("strconv.FormatInt:" + formatInt)
	// string -> int
	i, err := strconv.Atoi(str)
	//Itoa就是FormatInt(int64(vint), 10)
	fmt.Println("string -> int:" + strconv.Itoa(i))

	//4. float <-> string
	var vFloat = 2.22
	// f:格式 10:对于f来说，10是小数点后的位数。 64： 表示该小数是float64
	floatA := strconv.FormatFloat(vFloat, 'f', 10, 64)
	float, err := strconv.ParseFloat("2.3", 64)
	fmt.Println("strconv.FormatFloat:" + floatA)
	fmt.Println("strconv.ParseFloat:" + strconv.FormatFloat(float, 'f', 2, 64))

	//5. byte
	var bytes = []byte("hello")
	//bytes:[104 101 108 108 111]
	fmt.Printf("bytes:%v\n", bytes)
	bytesToStr := string(bytes)
	//bytes to string: hello
	fmt.Println("bytes to string:", bytesToStr)

}
